﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Ryder_US_Website.Utils;

namespace Ryder_US_Website.BUS
{
    public class CustomerBUS
    {
        public static string conString = ConfigurationManager.ConnectionStrings["breakers_RyderUSConnectionString"].ConnectionString;

        #region Select
        ///<summary>
        /// Gets all customers
        ///</summary>
        ///<param name="curPage">The current page.</param>
        ///<param name="rowsPerPage">The number of rows. Rows per page.</param>
        ///<param name="numPages">The number of pages.</param>
        ///<returns></returns>
        public static List<RUS_Customer> GetAll(int curPage, int rowsPerPage, ref int numPages)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (curPage < 1)
                    curPage = 1;
                if (rowsPerPage < 1)
                    rowsPerPage = 10;
                List<RUS_Customer> list = new List<RUS_Customer>();
                var query = from c in context.RUS_Customers
                            orderby c.Customer_username ascending
                            select c;
                numPages = (query.Count() / rowsPerPage) + 1;
                query = query.OrderBy(c => c.Customer_username);
                list = query.Skip((curPage - 1) * rowsPerPage).Take(rowsPerPage).ToList<RUS_Customer>();
                return list;
            }

        }

        ///<summary>
        /// Get all customers
        ///</summary>
        ///<returns>List of RUS_Customer.</returns>
        public static List<RUS_Customer> GetAll()
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                List<RUS_Customer> list = new List<RUS_Customer>();
                var query = from c in context.RUS_Customers
                            orderby c.Customer_username ascending
                            select c;
                list = query.ToList<RUS_Customer>();
                return list;
            }
        }

        ///<summary>
        /// Logins the specified username.
        ///</summary>
        ///<param name="username">The username.</param>
        ///<param name="password">The password</param>
        ///<returns></returns>
        public static RUS_Customer Login(String username, String password)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (String.IsNullOrEmpty(username))
                    throw new Exception("Tên đăng nhập không hợp lệ.");
                if (String.IsNullOrEmpty(password))
                    throw new Exception("Mật khẩu không hợp lệ.");
                RUS_Customer customer = new RUS_Customer();
                var query = from c in context.RUS_Customers
                            where (c.Customer_username == username) && (c.Customer_password == UtilHelpers.MD5Encode(password) && c.Customer_trash == 0)
                            select c;
                if (query.Count() != 0)
                {
                    customer = query.SingleOrDefault<RUS_Customer>();
                }
                else
                {
                    throw new Exception("Sai tên đăng nhập hoặc mật khẩu.");
                }
                return customer;
            }
        }

        ///<summary>
        ///Gets the active customers.
        ///</summary>
        ///<returns>List of active customers.</returns>
        public static List<RUS_Customer> GetActiveCustomer()
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                List<RUS_Customer> list = new List<RUS_Customer>();
                var query = from c in context.RUS_Customers
                            where (c.Customer_trash == 0)
                            orderby c.Customer_name
                            select c;
                list = query.ToList<RUS_Customer>();
                return list;
            }

        }

        ///<summary>
        ///Gets the active customers.
        ///</summary>
        ///<param name="curPage">The current page.</param>
        ///<param name="rowsPerPage">The number of rows. Rows per page.</param>
        ///<param name="numPages">The number of pages</param>
        ///<returns>List of active customers.</returns>
        public static List<RUS_Customer> GetActiveCustomer(int curPage, int rowsPerPage, int numPages)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (curPage < 1)
                    curPage = 1;
                if (rowsPerPage < 1)
                    rowsPerPage = 10;
                List<RUS_Customer> list = new List<RUS_Customer>();
                var query = from c in context.RUS_Customers
                            where (c.Customer_trash == 0)
                            orderby c.Customer_name
                            select c;
                numPages = (query.Count() / rowsPerPage) + 1;
                list = query.Skip((curPage - 1) * rowsPerPage).Take(rowsPerPage).ToList<RUS_Customer>();
                return list;
            }

        }

        ///<summary>
        ///Gets the inactive customers.
        ///</summary>
        ///<returns>list of inactive customers</returns>
        public static List<RUS_Customer> GetInactiveCustomer()
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                List<RUS_Customer> list = new List<RUS_Customer>();
                var query = from c in context.RUS_Customers
                            where (c.Customer_trash == 1)
                            orderby c.Customer_name
                            select c;
                list = query.ToList<RUS_Customer>();
                return list;
            }
        }

        ///<summary>
        ///Gets the inactive customers.
        ///</summary>
        ///<param name="curPage">The current page.</param>
        ///<param name="rowsPerPage">The number of rows. Rows per page.</param>
        ///<param name=" numPages">The number of pages.</param>
        ///<returns>List of inactive customers.</returns>
        public static List<RUS_Customer> GetInactiveCustomer(int curPage, int rowsPerPage, int numPages)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (curPage < 1)
                    curPage = 1;
                if (rowsPerPage < 1)
                    rowsPerPage = 10;
                List<RUS_Customer> list = new List<RUS_Customer>();
                var query = from c in context.RUS_Customers
                            where (c.Customer_trash == 1)
                            orderby c.Customer_name
                            select c;
                numPages = (query.Count() / rowsPerPage) + 1;
                list = query.Skip((curPage - 1) * rowsPerPage).Take(rowsPerPage).ToList<RUS_Customer>();
                return list;
            }
        }

        ///<summary>
        ///Gets the customer by id.
        ///</summary>
        ///<param name="id">The id.</param>
        ///<returns></returns>
        public static RUS_Customer GetCustomerByID(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Customers
                            where (c.Customer_ID == id)
                            select c;
                return query.SingleOrDefault<RUS_Customer>();
            }
        }

        ///<summary>
        ///Gets the active customer by id.
        ///</summary>
        ///<param name="id">The id.</param>
        ///<returns></returns>
        public static RUS_Customer GetActiveCustomerByID(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Customers
                            where (c.Customer_ID == id && c.Customer_trash == 0)
                            select c;
                return query.SingleOrDefault<RUS_Customer>();
            }
        }

        /// <summary>
        /// Checks the exist username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <returns></returns>
        public static Boolean checkExistUsername(String username)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Customers
                            where (c.Customer_username == username)
                            select c;
                if (query.Count() > 0)
                    return true;
                return false;
            }
        }

        /// <summary>
        /// Checks the exist username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static Boolean checkExistUsername(String username, int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Customers
                            where (c.Customer_username == username && c.Customer_ID != id)
                            select c;
                if (query.Count() > 0)
                    return true;
                return false;
            }
        }
        #endregion

        #region Insert
        ///<summary>
        /// Inserts the specified customer
        ///</summary>
        ///<param name="customer">The customer.</param>
        public static void Insert(RUS_Customer customer)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (customer != null)
                {
                    if (checkExistUsername(customer.Customer_username))
                        throw new Exception("Đã tồn tại khách hàng này");
                    context.RUS_Customers.InsertOnSubmit(customer);
                }
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể thêm khách hàng mới được.");
                }
            }
        }

        ///<summary>
        /// Inserts the specified name
        ///</summary>
        ///<param name="name">The customer's name.</param>
        ///<param name="username">The customer's username.</param>
        ///<param name="password">The customer's password.</param>
        ///<param name="address">The customer's address.</param>
        ///<param name="phone">The customer's phone.</param>
        ///<param name="banking">The customer's banking.</param>
        ///<returns></returns>
        public static void Insert(String name, String username, String password,
                                  String address, String phone, decimal doubt, String banking)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (String.IsNullOrEmpty(name))
                    throw new Exception("Tên không được để trống");
                if (String.IsNullOrEmpty(username))
                    throw new Exception("Tên đăng nhập không được để trống.");
                if (checkExistUsername(username))
                    throw new Exception("Đã tồn tại khách hàng này");
                if (String.IsNullOrEmpty(password))
                    throw new Exception("Mật khẩu không được để trống.");
                if (String.IsNullOrEmpty(banking))
                    throw new Exception("Tài khoản ngân hàng không được để trống");
                RUS_Customer customer = new RUS_Customer();
                customer.Customer_name = name;
                customer.Customer_username = username;
                customer.Customer_password = UtilHelpers.MD5Encode(password);
                customer.Customer_address = address;
                customer.Customer_phone = phone;
                customer.Customer_doubt = doubt;
                customer.Customer_banking = banking;
                customer.Customer_trash = 0;
                context.RUS_Customers.InsertOnSubmit(customer);
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Có lỗi xảy ra! Không thể thêm khách hàng mới được.");
                    throw new Exception(ex.Message);
                }
            }
        }
        #endregion

        #region Update
        ///<summary>
        ///Updates the specified customer
        ///</summary>
        ///<param name="customer">The customer.</param>
        ///<returns>customer</returns>
        public static RUS_Customer Update(RUS_Customer customer)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                try
                {
                    if (customer != null)
                    {
                        if (checkExistUsername(customer.Customer_username, customer.Customer_ID))
                            throw new Exception("Đã tồn tại khách hàng này");
                        context.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    throw new Exception("Có lỗi xảy ra! Không thể cập nhật khách hàng được.");
                }
                return customer;
            }
        }

        ///<summary>
        ///Updates the specified customer
        ///</summary>
        ///<param name="id">The id.</param>
        ///<param name="name">The customer's name.<param>
        ///<param name="password">The customer's password.</param>
        ///<param name="banking">The customer's banking.</param>
        ///<returns>customer</returns>
        public static RUS_Customer Update(int id, String name, String password, String banking)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (String.IsNullOrEmpty(name))
                    throw new Exception("Tên không được để trống.");
                if (String.IsNullOrEmpty(password))
                    throw new Exception("Mật khẩu không được để trống.");
                RUS_Customer customer = new RUS_Customer();
                var query = from cs in context.RUS_Customers
                            where (cs.Customer_ID == id)
                            select cs;
                customer = query.SingleOrDefault<RUS_Customer>();
                if (customer != null)
                {
                    customer.Customer_name = name;
                    customer.Customer_banking = banking;
                    customer.Customer_password = UtilHelpers.MD5Encode(password);
                }
                try
                {
                    //context.RUS_Customers.Attach(customer);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    throw new Exception("Có lỗi xảy ra! Không thể cập nhật khách hàng được.");
                }
                return customer;
            }
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="name">The customer's name.</param>
        /// <param name="username">The customer's username.</param>
        /// <param name="password">The customer's password.</param>
        /// <param name="address">The customer's address.</param>
        /// <param name="phone">The customer's phone.</param>
        /// <param name="banking">The customer's banking.</param>
        /// <param name="dout">The customer's dout.</param>
        /// <param name="trash">The customer's trash.</param>
        /// <returns></returns>
        public static RUS_Customer Update(int id, String name, String username, String password,
                                  String address, String phone, String banking, decimal dout, int trash)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (String.IsNullOrEmpty(name))
                    throw new Exception("Tên không được để trống.");
                if (String.IsNullOrEmpty(username))
                    throw new Exception("Tên đăng nhập không được để trống.");
                if (checkExistUsername(username, id))
                    throw new Exception("Đã tồn tại khách hàng này");
                if (String.IsNullOrEmpty(password))
                    throw new Exception("Mật khẩu không được để trống.");
                if (String.IsNullOrEmpty(banking))
                    throw new Exception("Tài khoản ngân hàng không được để trống.");
                RUS_Customer customer = new RUS_Customer();
                var query = from cs in context.RUS_Customers
                            where (cs.Customer_ID == id)
                            select cs;
                customer = query.SingleOrDefault<RUS_Customer>();
                if (customer != null)
                {
                    customer.Customer_name = name;
                    customer.Customer_username = username;
                    customer.Customer_password = UtilHelpers.MD5Encode(password);
                    customer.Customer_address = address;
                    customer.Customer_phone = phone;
                    customer.Customer_banking = banking;
                    customer.Customer_doubt = dout;
                    customer.Customer_trash = trash;
                }
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Có lỗi xảy ra! Không thể cập nhật khách hàng được.");
                    throw new Exception(ex.Message);
                }
                return customer;
            }
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="name">The customer's name.</param>
        /// <param name="username">The customer's username.</param>
        /// <param name="address">The customer's address.</param>
        /// <param name="phone">The customer's phone.</param>
        /// <param name="banking">The customer's banking.</param>
        /// <param name="doubt">The customer's doubt.</param>
        /// <param name="trash">The customer's trash.</param>
        /// <returns></returns>
        public static RUS_Customer Update(int id, String name, String username, String address,
                                            String phone, String banking, decimal doubt, int trash)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (String.IsNullOrEmpty(name))
                    throw new Exception("Tên không được để trống.");
                if (String.IsNullOrEmpty(username))
                    throw new Exception("Tên đăng nhập không được để trống.");
                if (checkExistUsername(username, id))
                    throw new Exception("Đã tồn tại khách hàng này");
                if (String.IsNullOrEmpty(banking))
                    throw new Exception("Tài khoản ngân hàng không được để trống.");
                RUS_Customer customer = new RUS_Customer();
                var query = from cs in context.RUS_Customers
                            where (cs.Customer_ID == id)
                            select cs;
                customer = query.SingleOrDefault<RUS_Customer>();
                if (customer != null)
                {
                    customer.Customer_name = name;
                    customer.Customer_username = username;
                    customer.Customer_address = address;
                    customer.Customer_phone = phone;
                    customer.Customer_banking = banking;
                    customer.Customer_doubt = doubt;
                    customer.Customer_trash = trash;
                }
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Có lỗi xảy ra! Không thể cập nhật khách hàng được.");
                    throw new Exception(ex.Message);
                }
                return customer;
            }
        }
        #endregion

        #region Restore & Delete
        ///<summary>
        ///Deletes the specified customer
        ///</summary>
        ///<param name="customer">The customer.</param>
        ///<returns>customer</returns>
        public static RUS_Customer Delete(RUS_Customer customer)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                customer.Customer_trash = 1;
                try
                {
                    context.RUS_Customers.Attach(customer);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa khách hàng được.");
                }
                return customer;
            }
        }

        ///<summary>
        ///Restores the specified customer
        ///</summary>
        ///<param name="customer">The customer.</param>
        ///<returns>customer</returns>
        public static RUS_Customer Restore(RUS_Customer customer)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (customer != null)
                    return null;
                customer.Customer_trash = 0;
                try
                {
                    context.RUS_Customers.Attach(customer);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi khách hàng được.");
                }
                return customer;
            }
        }

        ///<summary>
        ///Deletes the specified customer by id
        ///</summary>
        ///<param name="id">The customer's id.</param>
        ///<returns>customer</returns>
        public static RUS_Customer Delete(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Customers
                            where (c.Customer_ID == id)
                            select c;
                RUS_Customer customer = query.SingleOrDefault<RUS_Customer>();
                customer.Customer_trash = 1;
                try
                {
                    context.RUS_Customers.Attach(customer);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa khách hàng được.");
                }
                return customer;
            }
        }

        ///<summary>
        ///Restores the specified customer by id
        ///</summary>
        ///<param name="id">The customer's id.</param>
        ///<returns>customer</returns>
        public static RUS_Customer Restore(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Customers
                            where (c.Customer_ID == id)
                            select c;
                RUS_Customer customer = query.SingleOrDefault<RUS_Customer>();
                customer.Customer_trash = 0;
                try
                {
                    context.RUS_Customers.Attach(customer);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi khách hàng được.");
                }
                return customer;
            }
        }

        ///<summary>
        ///Deletes multiple specified customers.
        ///</summary>
        ///<param name="ids">The customer's ids.</param>
        ///<returns>The list of customer</returns>
        public static List<RUS_Customer> Delete(int[] ids)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Customers
                            where (ids.Contains(c.Customer_ID))
                            select c;
                List<RUS_Customer> list = query.ToList<RUS_Customer>();
                foreach (RUS_Customer c in list)
                {
                    c.Customer_trash = 1;
                }
                try
                {
                    context.RUS_Customers.AttachAll<RUS_Customer>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa khách hàng được.");
                }
                return list;
            }
        }

        ///<summary>
        ///Restores multiple specified customers.
        ///</summary>
        ///<param name="ids">The customer's ids.</param>
        ///<returns>The list of customer</returns>
        public static List<RUS_Customer> Restore(int[] ids)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Customers
                            where (ids.Contains(c.Customer_ID))
                            select c;
                List<RUS_Customer> list = query.ToList<RUS_Customer>();
                foreach (RUS_Customer c in list)
                {
                    c.Customer_trash = 0;
                }
                try
                {
                    context.RUS_Customers.AttachAll<RUS_Customer>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa khách hàng được.");
                }
                return list;
            }
        }


        /// <summary>
        /// Deletes multiple specified customer.
        /// </summary>
        /// <param name="ids">The ids.</param>
        public static List<RUS_Customer> Delete(List<RUS_Customer> customers)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                foreach (RUS_Customer c in customers)
                {
                    c.Customer_trash = 1;
                }
                try
                {
                    context.RUS_Customers.AttachAll<RUS_Customer>(customers);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không xóa khách hàng được.");
                }
                return customers;
            }
        }

        /// <summary>
        /// Restores multiple specified customer.
        /// </summary>
        /// <param name="ids">The ids.</param>
        public static List<RUS_Customer> Restore(List<RUS_Customer> customers)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                foreach (RUS_Customer c in customers)
                {
                    c.Customer_trash = 0;
                }
                try
                {
                    context.RUS_Customers.AttachAll<RUS_Customer>(customers);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi khách hàng được.");
                }
                return customers;
            }
        }
        ///<summary>
        /// Delete or restores Customer.
        /// </summary>
        /// <param name="id">The id.</param>
        public static RUS_Customer DeleteRestore(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_Customers
                            where (cs.Customer_ID == id)
                            select cs;
                RUS_Customer customer = query.SingleOrDefault<RUS_Customer>();
                if (customer.Customer_trash == 0)
                {
                    foreach (RUS_Cargo cg in customer.RUS_Cargos)
                    {
                        if (cg.Cargo_trash == 0)
                        {
                            throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                        }
                    }
                }
                customer.Customer_trash = 1 - customer.Customer_trash;
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Không thể phục hồi tình trạng khách hàng");
                }
                return customer;
            }
        }
        #endregion
    }
}
